package com.jyx.chain.web.filterchain;

import java.util.ArrayList;
import java.util.List;

import com.jyx.chain.web.Filter;
import com.jyx.chain.web.request.Request;
import com.jyx.chain.web.response.Response;

/**
 * 一个完整的filterCahin
 * @author Administrator
 *
 */
public class FilterChain implements Filter {

	private List<Filter> filters = new ArrayList<Filter>();
	private int index = 0;
	/**
	 * 添加一个过滤器
	 * @param f
	 */
	public FilterChain addFilter(Filter f){
		filters.add(f);
		return this;
	}
	
	/**
	 * 执行自己的过滤方法
	 * @param msg
	 * @return
	 */
	@Override
	public void doFilter(Request req, Response res,FilterChain fc) {
		if(index == filters.size()) return;
		
		Filter f =filters.get(index);
		index ++;
		f.doFilter(req, res, fc);
	}
	
}
